//
//  PCTabBarController.m
//  PCDemo
//
//  Created by aaa on 16/7/12.
//  Copyright © 2016年 FanShuHan. All rights reserved.
//

#import "PCTabBarController.h"
#import "PCTabBar.h"
#import "ClassificationViewController.h"
#import "MeViewController.h"
#import "HomeViewController.h"
#import "ShoppingCarViewController.h"
#import "PCNavigationViewController.h"
@implementation PCTabBarController
// 只会调用一次
+ (void)load
{
    // 获取哪个类中UITabBarItem
    UITabBarItem *item = [UITabBarItem appearanceWhenContainedIn:self, nil];
    
    // 设置按钮选中标题的颜色:富文本:描述一个文字颜色,字体,阴影,空心,图文混排
    // 创建一个描述文本属性的字典
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSForegroundColorAttributeName] = [UIColor blackColor];
    [item setTitleTextAttributes:attrs forState:UIControlStateSelected];
    
    // 设置字体尺寸:只有设置正常状态下,才会有效果
    NSMutableDictionary *attrsNor = [NSMutableDictionary dictionary];
    attrsNor[NSFontAttributeName] = [UIFont systemFontOfSize:13];
    [item setTitleTextAttributes:attrsNor forState:UIControlStateNormal];
}
#pragma mark - 生命周期方法
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 1 添加子控制器(5个子控制器) -> 自定义控制器 -> 划分项目文件结构
    [self setupAllChildViewController];
    
    // 2 设置tabBar上按钮内容 -> 由对应的子控制器的tabBarItem属性
    [self setupAllTitleButton];
    
    // 3.自定义tabBar
    [self setupTabBar];
    PCLog(@"%zd",self.viewControllers);
}

#pragma mark - 自定义tabBar
- (void)setupTabBar
{
    PCTabBar *tabBar = [[PCTabBar alloc] init];
    [self setValue:tabBar forKey:@"tabBar"];
}

/*
 Changing the delegate of a tab bar 【managed by a tab bar controller】 is not allowed.
 被UITabBarController所管理的UITabBar的delegate是不允许修改的
 */

#pragma mark - 添加所有子控制器
- (void)setupAllChildViewController
{
    // 主页
    HomeViewController *newVc = [[HomeViewController alloc] init];
    PCNavigationViewController *nav1 = [[PCNavigationViewController alloc] initWithRootViewController:newVc];
    [self addChildViewController:nav1];
    
    // 分类
    ClassificationViewController *essenceVc = [[ClassificationViewController alloc] init];
    PCNavigationViewController *nav = [[PCNavigationViewController alloc] initWithRootViewController:essenceVc];
    [self addChildViewController:nav];
    
    // 购物车
    ShoppingCarViewController *ftVc = [[ShoppingCarViewController alloc] init];
    PCNavigationViewController *nav3 = [[PCNavigationViewController alloc] initWithRootViewController:ftVc];
    [self addChildViewController:nav3];
    
    // 我
    MeViewController *MeVc = [[MeViewController alloc] init];
    PCNavigationViewController *nav4 = [[PCNavigationViewController alloc] initWithRootViewController:MeVc];
    [self addChildViewController:nav4];
    
//    // 我
//    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:NSStringFromClass([XMGMeViewController class]) bundle:nil];
//    // 加载箭头指向控制器
//    XMGMeViewController *meVc = [storyboard instantiateInitialViewController];
//    XMGNavigationViewController *nav4 = [[XMGNavigationViewController alloc] initWithRootViewController:meVc];
//    [self addChildViewController:nav4];
}

// 设置tabBar上所有按钮内容
- (void)setupAllTitleButton
{
    // 0:nav
    UINavigationController *nav = self.childViewControllers[0];
    nav.tabBarItem.title = @"首页";
    nav.tabBarItem.image = [UIImage imageNamed:@"tabBar_essence_icon"];
    // 快速生成一个没有渲染图片
    nav.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_essence_click_icon"];
    
    // 1:新帖
    UINavigationController *nav1 = self.childViewControllers[1];
    nav1.tabBarItem.title = @"分类";
    nav1.tabBarItem.image = [UIImage imageNamed:@"tabBar_new_icon"];
    nav1.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_new_click_icon"];
    
    // 3.关注
    UINavigationController *nav3 = self.childViewControllers[2];
    nav3.tabBarItem.title = @"购物车";
    nav3.tabBarItem.image = [UIImage imageNamed:@"tabBar_friendTrends_icon"];
    nav3.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_friendTrends_click_icon"];
    
    // 4.我
    UINavigationController *nav4 = self.childViewControllers[3];
    nav4.tabBarItem.title = @"我";
    nav4.tabBarItem.image = [UIImage imageNamed:@"tabBar_me_icon"];
    nav4.tabBarItem.selectedImage = [UIImage imageOriginalWithName:@"tabBar_me_click_icon"];
}

@end
